package frame;

import bean.Parametrizacao;
import controle.PrevisaoPJ;
import dao.TarefaDAO;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.swing.JOptionPane;


public class PrevisaoFrame extends javax.swing.JFrame {

    String min_hora, hora_extra;
    double valor_min; 
         double   valor_hora  ;
    DecimalFormat df = new DecimalFormat("R$ #,##0.00");//formato  
    DecimalFormat df2 = new DecimalFormat("#,##0.00");//formato  
    SimpleDateFormat data = new SimpleDateFormat("dd/MM/yyyy");

    public PrevisaoFrame() {
        initComponents();
        setParameter();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_enviar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jDateChooser_datafinal = new com.toedter.calendar.JDateChooser();
        jDateChooser_datainicial = new com.toedter.calendar.JDateChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jFormattedTextField_valorhora = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setTitle("Rendimentos");
        setResizable(false);
        getContentPane().setLayout(null);

        jButton_enviar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton_enviar.setText("Consultar");
        jButton_enviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_enviarActionPerformed(evt);
            }
        });
        getContentPane().add(jButton_enviar);
        jButton_enviar.setBounds(50, 140, 100, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("    PREVISÃO  MENSAL");
        jLabel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
        });
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 10, 190, 30);

        jDateChooser_datafinal.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        getContentPane().add(jDateChooser_datafinal);
        jDateChooser_datafinal.setBounds(100, 80, 100, 24);

        jDateChooser_datainicial.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        getContentPane().add(jDateChooser_datainicial);
        jDateChooser_datainicial.setBounds(100, 50, 100, 24);

        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        jTextArea1.setForeground(new java.awt.Color(0, 51, 204));
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 170, 190, 140);

        jFormattedTextField_valorhora.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jFormattedTextField_valorhora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        jFormattedTextField_valorhora.setText("0");
        jFormattedTextField_valorhora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextField_valorhoraActionPerformed(evt);
            }
        });
        jFormattedTextField_valorhora.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jFormattedTextField_valorhoraFocusGained(evt);
            }
        });
        getContentPane().add(jFormattedTextField_valorhora);
        jFormattedTextField_valorhora.setBounds(100, 110, 100, 20);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Valor Hora");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 110, 80, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Data Inicio");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 50, 80, 20);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Data Fim");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 80, 80, 20);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-214)/2, (screenSize.height-349)/2, 214, 349);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_enviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_enviarActionPerformed
       
        if ( jDateChooser_datainicial.getDate() == null ){
           JOptionPane.showMessageDialog(null, "Data Inicial Invalida !");
        }
        else if ( jDateChooser_datafinal.getDate() == null ){
           JOptionPane.showMessageDialog(null, "Data Final Invalida !");
        }
        else if ( jDateChooser_datainicial.getDate().after(jDateChooser_datafinal.getDate( ) ) ){
           JOptionPane.showMessageDialog(null, "Data inicial maor que data final !");
        }        
        else{
         PrevisaoPJ pj = new PrevisaoPJ();
            
          Map<String, Integer> mapa = pj.previsao( jDateChooser_datainicial.getDate(), jDateChooser_datafinal.getDate() ); 
          
          String hora_valor = jFormattedTextField_valorhora.getText( ).replace(",", ".") ;
          valor_hora =  Double.parseDouble( hora_valor );
          int dias = mapa.get( "semana" ) ;
          double total = ( dias * 8 ) * valor_hora;
                  
          
         String txt =
                    "De "  + data.format( jDateChooser_datainicial.getDate() ) + "\n" +
                    "Ate " + data.format(jDateChooser_datafinal.getDate() )  + "\n" +
                     mapa.get( "domingos" )+ " Domingos " +"\n" +
                     mapa.get( "sabados"  )+ " Sabados"   +"\n" +
                     mapa.get( "semana"   )+ " Semana"    +"\n" +
                     mapa.get( "feriados" )+ " Feriados"  +"\n" +
                     "A Receber " + df.format(total);                      
         
         jTextArea1.setText(txt);
            
         updateParametr();
        }
    }//GEN-LAST:event_jButton_enviarActionPerformed

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
 
             java.util.Date di = jDateChooser_datainicial.getDate();
             int mes = di.getMonth()+1;
             di.setMonth(mes);
             jDateChooser_datainicial.setDate(di);
             
             java.util.Date df = jDateChooser_datafinal.getDate();
             int mesf = df.getMonth()+1;
             df.setMonth(mesf);
             jDateChooser_datafinal.setDate(df);               
        
    }//GEN-LAST:event_jLabel3MouseClicked

    private void jFormattedTextField_valorhoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextField_valorhoraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedTextField_valorhoraActionPerformed

    private void jFormattedTextField_valorhoraFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField_valorhoraFocusGained
        jFormattedTextField_valorhora.requestFocus();
    }//GEN-LAST:event_jFormattedTextField_valorhoraFocusGained
 
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PrevisaoFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_enviar;
    private com.toedter.calendar.JDateChooser jDateChooser_datafinal;
    private com.toedter.calendar.JDateChooser jDateChooser_datainicial;
    private javax.swing.JFormattedTextField jFormattedTextField_valorhora;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

     public void updateParametr( ) {

    	TarefaDAO daoprm1 =  new TarefaDAO();
    	TarefaDAO daoprm2 =  new TarefaDAO();
    	TarefaDAO daoprm3 =  new TarefaDAO();
    	TarefaDAO daoprm4 =  new TarefaDAO();
    	
    	try {
			Map<String, String> mapa = daoprm1.getPrm();
			
			String dataIniPrm = mapa.get("inicio");
			String dataFimPrm = mapa.get("fim");
			String valorPrm = mapa.get("valor");
			
			String dataFimFrm =	data.format(jDateChooser_datafinal.getDate().getTime());
			String dataIniFrm =	data.format(jDateChooser_datainicial.getDate().getTime());
			String valorFrm = jFormattedTextField_valorhora.getText();
			
			if ( !valorFrm.equals( valorPrm ) ) {
				Parametrizacao param = new Parametrizacao();
				param.setParametro( "valor" );
				param.setValor( valorFrm );
				daoprm2.updatePrm( param );
			}
			
			if ( !dataIniFrm.equals( dataIniPrm ) ) {
				Parametrizacao param1 = new Parametrizacao();
				param1.setParametro( "inicio" );
				param1.setValor( dataIniFrm );
				daoprm3.updatePrm( param1 );
			}
			
			if ( !dataFimFrm.equals( dataFimPrm ) ) {
				Parametrizacao param2 = new Parametrizacao();
				param2.setParametro( "fim" );
				param2.setValor( dataFimFrm );
				daoprm4.updatePrm( param2 );
			}
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
    
    public void setParameter( ) {
    	TarefaDAO daoprm1 =  new TarefaDAO();
    	try {
			Map<String, String> mapa = daoprm1.getPrm();
			String dataIniPrm = mapa.get("inicio");
			String dataFimPrm = mapa.get("fim");
			String valorPrm = mapa.get("valor");
			Date dataini =  data.parse(dataIniPrm);
			Date datafim = data.parse(dataFimPrm);
			jFormattedTextField_valorhora.setText(valorPrm);
			jDateChooser_datafinal.setDate( datafim );
			jDateChooser_datainicial.setDate( dataini);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
    
    
    
}
